package com.example.a828502.ormlite.bean;

import android.content.Context;

import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.DeleteBuilder;
import com.j256.ormlite.stmt.UpdateBuilder;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 828502 on 2016/5/10.
 */
public class TestDao {
    private Dao<TestBean,Integer> mTestDao;

    public TestDao(Context context){
        try {
            mTestDao = DatabaseHelper.getHelper(context).getDao(TestBean.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询所有数据
     */

    public List<TestBean> querySQLData(){
        try {
            List<TestBean> datas= new ArrayList<TestBean>();
//            datas =  mTestDao.queryForAll(); //方法可用

//            datas = mTestDao.queryBuilder()
//                    .distinct()
//                    .selectColumns("id")
//                    .selectColumns("test_id")
//                    .selectColumns("test_name")
//                    .where()
//                    .eq("test_name", "粉粉2")
//                    .query();                //查询某一条数据
//            return datas;

            return mTestDao.queryBuilder().query();//查询全部数据


        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 插入一条cityDictBean数据
     *
     * @param mTestBean
     * @return
     */
    public boolean addTestBean(TestBean mTestBean) {
        try {
            return mTestDao.create(mTestBean) == 1;

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 更新一条cityDictBean数据
     *
     * @param mTestBean
     * @return
     */
    public boolean updateTestBean(TestBean mTestBean) {
        try {
            UpdateBuilder updateBuilder=mTestDao.updateBuilder();
            updateBuilder.updateColumnValue("test_name",mTestBean.test_name)
                    .where()
                    .eq("test_id",mTestBean.test_id);
            updateBuilder.update();
            return true;
//            return mTestDao.update(mTestBean) == 1;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
    /**
     * 删除一条数据
     *
     * @param mTestBean
     * @return
     */
    public boolean deleteTestBean(TestBean mTestBean) {
        try {
            DeleteBuilder deleteBuilder = mTestDao.deleteBuilder();
            deleteBuilder.where()
            .eq("test_id",mTestBean.test_id);
            deleteBuilder.delete();

            return true;
//            return mTestDao.delete(mTestBean) == 1;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

}
